home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / misc / sizeof.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-19  |  1.2 KB  |  66 lines

  1. /*
  2.  * sizeof - report total size of files
  3.  *
  4.  * You might ask, why couldn't this be a shell program invoking ls -l?
  5.  * Well, apart from the variations in the format of ls -l, there's also
  6.  * the problem that the Berkloid ls -l doesn't follow symlinks unless
  7.  * asked to (with an unportable option).
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13.  
  14. int debug = 0;
  15. int indiv = 0;
  16. char *progname;
  17.  
  18. extern void error(), exit();
  19.  
  20. /*
  21.  - main - do it all
  22.  */
  23. main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     int c;
  28.     int errflg = 0;
  29.     struct stat statbuf;
  30.     extern int optind;
  31.     extern char *optarg;
  32.     register off_t total = 0;
  33.  
  34.     progname = argv[0];
  35.  
  36.     while ((c = getopt(argc, argv, "ix")) != EOF)
  37.         switch (c) {
  38.         case 'i':    /* Individual files. */
  39.             indiv = 1;
  40.             break;
  41.         case 'x':    /* Debugging. */
  42.             debug++;
  43.             break;
  44.         case '?':
  45.         default:
  46.             errflg++;
  47.             break;
  48.         }
  49.     if (errflg || optind >= argc) {
  50.         fprintf(stderr, "usage: %s ", progname);
  51.         fprintf(stderr, "file ...\n");
  52.         exit(2);
  53.     }
  54.  
  55.     for (; optind < argc; optind++)
  56.         if (stat(argv[optind], &statbuf) >= 0) {
  57.             total += statbuf.st_size;
  58.             if (debug || indiv)
  59.                 printf("%s %ld\n", argv[optind],
  60.                         (long)statbuf.st_size);
  61.         }
  62.     if (!indiv)
  63.         printf("%ld\n", (long)total);
  64.     exit(0);
  65. }
  66.